package FlickrGeoTagr {
	
	import FlickrGeoTagr.Flickr.*;
	import FlickrGeoTagr.GPSReaders.RMCReader;
	
	import com.google.maps.*;
	import com.google.maps.overlays.*;
	
	import flash.events.*;
	import flash.net.FileReference;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IViewCursor;
	import mx.controls.DataGrid;
	import mx.controls.HSlider;
	import mx.core.*;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	
	public class FlickrGeoTagrApplication extends WindowedApplication {	
		public var myFileReference:FileReference = new FileReference(); 
		
		public var map:Map;
		public var hourOffsetSlider:HSlider;
		public var minuteOffsetSlider:HSlider;
		
		public var gpsGrid:DataGrid;
		
		private var _lastMarker:Marker;
		
		private var _flickrAuthorization:FlickrAuthorization;
		private var _flickrPhotos:FlickrPhotos;
		private var _rmcReader:RMCReader;
		
		[Bindable]
		public var gpsGridData:ArrayCollection;
		[Bindable]
		public var flickrGridData:ArrayCollection;
		
		public function FlickrGeoTagrApplication() {
			this.addEventListener(FlexEvent.CREATION_COMPLETE, handleCreationComplete);
		}
		
		public function handleCreationComplete(event:FlexEvent):void {
			_flickrAuthorization = new FlickrAuthorization();
			_flickrAuthorization.openAuthorization(this);
		}
		
		public function browseForLog():void {
			myFileReference.addEventListener(Event.SELECT, openLog);
			myFileReference.browse();
		}
		
		public function openLog(event:Event):void {
			myFileReference.removeEventListener(Event.SELECT, openLog);
			myFileReference.addEventListener(Event.COMPLETE, handleLogLoaded);
			myFileReference.load();
		}
		
		private function handleLogLoaded(event:Event):void {
			_rmcReader = new RMCReader(myFileReference.data.toString());
			gpsGridData = new ArrayCollection(_rmcReader.Log);
			
			getFlickrPhotos();
		}
		
		public function getFlickrPhotos():void {
			_flickrPhotos = new FlickrPhotos(_flickrAuthorization);
			_flickrPhotos.addEventListener(Event.COMPLETE, handleFlickrPhotosRetrieved);
			_flickrPhotos.loadWithLog(_rmcReader.Log);
		}
		
		public function handleFlickrPhotosRetrieved($event:Event):void {
			_flickrPhotos.removeEventListener(Event.COMPLETE, handleFlickrPhotosRetrieved);
			flickrGridData = new ArrayCollection(_flickrPhotos.PhotoArray);
			trace("handleFlickrPhotosRetrieved");
		}
		
		public function showOnMap(selectedItem:Object):void {
			var latlng:LatLng = new LatLng(Number(selectedItem.Latitude),Number(selectedItem.Longitude));
			map.setCenter(latlng, 14, MapType.NORMAL_MAP_TYPE);
			
			if (_lastMarker != null) {
				map.removeOverlay(_lastMarker);
			}
			
			_lastMarker = new Marker(latlng);
			map.addOverlay(_lastMarker);
		}
		
		public function handleItemClick(event:ListEvent):void {
			var selectedItem:Object = event.currentTarget.selectedItem;
			showOnMap(selectedItem);
		}
		
		public function handleFlickrItemClick(event:ListEvent):void {
			var selectedItem:Object = event.currentTarget.selectedItem;
			var foundAt:Number = 0;
			var dataProvider:Object = gpsGrid.dataProvider;
			var cursor:IViewCursor = dataProvider.createCursor();
			
			var i:Number = 0;
			while(!cursor.afterLast) {
				if (cursor.current.Id == selectedItem.LogId) {
					foundAt = i;
					break;
				}
				
				i++;
				cursor.moveNext();
			}
			
			gpsGrid.selectedIndex = foundAt;
			gpsGrid.validateNow();
			gpsGrid.scrollToIndex(foundAt);
			showOnMap(gpsGrid.selectedItem);
		}
		
		public function updateTimestamps():void {
			var hourOffset:Number = hourOffsetSlider.value;
			var minuteOffset:Number = minuteOffsetSlider.value;
			
			_rmcReader.offsetTime(hourOffset, minuteOffset)
			
			gpsGridData.refresh();
			
			getFlickrPhotos();
		}
		
		public function processPhotos():void {
			//_flickrPhotos.processPhotos();
		}
	}
}